<script setup lang="ts">
import { ref } from "vue";
import { useLog } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";

import Refresh from "~icons/ep/refresh";
import Export from "~icons/ep/folder-opened";

defineOptions({
  name: "SystemLog"
});

const formRef = ref();

const {
  form,
  loading,
  columns,
  dataList,
  pagination,
  deviceDetection,
  onSearch,
  resetForm,
  handleSizeChange,
  handleCurrentChange,
  exportLog
} = useLog();
</script>
<template>
  <div :class="['flex', 'justify-between', deviceDetection() && 'flex-wrap']">
    <div :class="[deviceDetection() ? ['w-full', 'mt-2'] : 'w-full']">
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
      >
        <el-form-item label="请求地址：" prop="requestUrl">
          <el-input
            v-model="form.requestUrl"
            placeholder="请输入请求地址"
            clearable
            class="!w-[180px]"
          />
        </el-form-item>
        <el-form-item label="请求内容：" prop="requestContent">
          <el-input
            v-model="form.requestContent"
            placeholder="请输入请求内容"
            clearable
            class="!w-[180px]"
          />
        </el-form-item>
        <el-form-item label="请求时间：" prop="requestDate">
          <el-date-picker
            v-model="form.requestDate"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            :clearable="false"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri:search-line')"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <PureTableBar title="接口日志" :columns="columns" @refresh="onSearch">
        <template #buttons>
          <el-button :icon="useRenderIcon(Export)" @click="exportLog">
            导出
          </el-button>
        </template>
        <template v-slot="{ size, dynamicColumns }">
          <pure-table
            ref="tableRef"
            row-key="id"
            adaptive
            :adaptiveConfig="{ offsetBottom: 108 }"
            align-whole="center"
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="dynamicColumns"
            :pagination="{ ...pagination, size }"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @page-current-change="handleCurrentChange"
            @page-size-change="handleSizeChange"
          />
        </template>
      </PureTableBar>
    </div>
  </div>
</template>
